﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;
using System.Globalization;
using System.Threading;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class AddClientToGroup : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int groupId = 0;
                if (Request.QueryString["GroupId"] != null)
                {
                    groupId = int.Parse(Request.QueryString["GroupId"]);
                }
                NxGroup nxGroup = ServiceLocator.IClientService.GetNxGroupById(groupId);
                this.lbGroupName.Text = nxGroup.GroupName;
                DataBind_ListBoxClient();
                DataBind_ListBoxGroupClient();               
            }
        }

        protected void DataBind_ListBoxClient()
        {
            if (Session["MerchantId"] != null)
            {
                IList<NxClient> ClientCollection = ServiceLocator.IClientService.GetNxClientNotInGroup(int.Parse(Request.QueryString["GroupId"]),int.Parse(Session["MerchantId"].ToString()));
                this.lboxClient.DataSource = ClientCollection;
                foreach (NxClient client in ClientCollection)
                {
                    this.lboxClient.Items.Add(new ListItem(client.UserName + "，" + (client.Company.Length > 15 ? client.Company.Substring(0, 15) : client.Company), client.ClientId.ToString()));
                }
            }
        }

        protected void DataBind_ListBoxGroupClient()
        {
            if (Session["MerchantId"] != null)
            {
                IList<NxClient> ClientCollection = ServiceLocator.IClientService.GetNxClientInGroup(int.Parse(Request.QueryString["GroupId"]), int.Parse(Session["MerchantId"].ToString()));
                this.lboxGroupClient.DataSource = ClientCollection;
                foreach (NxClient client in ClientCollection)
                {
                    this.lboxGroupClient.Items.Add(new ListItem(client.UserName + "，" + (client.Company.Length > 15 ? client.Company.Substring(0, 15) : client.Company), client.ClientId.ToString()));
                }
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lboxGroupClient.Items.Count; i++)
            {
                if (this.lboxGroupClient.Items[i].Selected == true)
                {
                    int groupId = int.Parse(Request.QueryString["GroupId"]);
                    int clientId = int.Parse(this.lboxGroupClient.Items[i].Value);
                    bool bol = ServiceLocator.IClientService.DeleteNxGroupClient(groupId, clientId);
                }
                if (i == this.lboxGroupClient.Items.Count - 1)
                {
                    Response.Redirect("AddClientToGroup.aspx?GroupId=" + Request.QueryString["GroupId"]);
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            NxGroupClient nxGroupClient = new NxGroupClient();
            for (int i = 0; i < this.lboxClient.Items.Count; i++)
            {
                if (this.lboxClient.Items[i].Selected == true)
                {
                    nxGroupClient.ClientId = int.Parse(this.lboxClient.Items[i].Value);
                    nxGroupClient.GroupId = int.Parse(Request.QueryString["GroupId"]);
                    bool bol = ServiceLocator.IClientService.InsertNxGroupClient(nxGroupClient);
                }
                if (i == this.lboxClient.Items.Count - 1)
                {
                    Response.Redirect("AddClientToGroup.aspx?GroupId=" + Request.QueryString["GroupId"]);
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Response.Redirect("GroupList.aspx");
        }
    }
}
